package com.demo2do.fms.admin.dao.authority;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;
import com.demo2do.fms.entity.authority.Resource;
import com.demo2do.fms.entity.authority.ResourceType;
import com.demo2do.fms.entity.authority.RoleTemplate;

/**
 * 
 * @author Ysy
 */
@Dao("hibernate")
public interface ResourceDao {
    
    /**
     * count resource by template
     * 
     * @param template
     * @return
     */
    @Query("SELECT COUNT(*) FROM RoleTemplateResource r WHERE r.template = :template")
    public int count(@Variable("template") RoleTemplate template);
    
    /**
     * list resources by role_template, beginIndex and maxResult
     * 
     * @param template
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("SELECT r.resource FROM RoleTemplateResource r WHERE r.template = :template")
    public List<Resource> list(@Variable("template") RoleTemplate template, @Variable("beginIndex")int beginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * list resources' name by role_template and resource_type
     * 
     * @param template
     * @param type
     * @return
     */
    @Query("SELECT r.resource.name FROM RoleTemplateResource r WHERE r.template = :template AND r.resource.type = :type ORDER BY r.resource.id ASC")
    public List<String> list(@Variable("template") RoleTemplate template, @Variable("type")ResourceType type);
    
    /**
     * list resources by resource_types
     * 
     * @param types
     * @return
     */
    @Query("FROM Resource r WHERE r.type IN (:types)")
    public List<Resource> list(@Variable("types")List<ResourceType> types);
    
    /**
     * count resource by role_code
     * 
     * @param roleCode
     * @return
     */
    @Query("SELECT COUNT(r.resource) FROM RoleResource r WHERE r.roleCode = :roleCode AND r.role.disabled = false")
    public int count(@Variable("roleCode") String roleCode);
    
    /**
     * list resources by role_code, beginIndex and maxResult
     * 
     * @param roleCode
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("SELECT r.resource FROM RoleResource r WHERE r.roleCode = :roleCode ORDER BY r.resource.id ASC")
    public List<Resource> list(@Variable("roleCode") String roleCode, @Variable("beginIndex")int beginIndex, @Variable("maxResult")int maxResult);
    
    /**
     * list resources by role_code
     * 
     * @param roleCode
     * @return
     */
    @Query("SELECT r.resource FROM RoleResource r WHERE r.roleCode = :roleCode ORDER BY r.resource.id ASC")
    public List<Resource> list(@Variable("roleCode") String roleCode);

    /**
     * @param roleCode
     */
    @Execute("DELETE FROM RoleResource r WHERE r.roleCode = :roleCode")
    public void delete(@Variable("roleCode") String roleCode);
    
}
